package net.pimpas.web.jsf.renderers.panelgrid.ajax;

import java.io.IOException;

import javax.faces.component.UIComponent;
import javax.faces.component.html.HtmlPanelGrid;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
import javax.faces.render.Renderer;

import net.pimpas.web.jsf.renderers.AjaxRendererBase;
import net.pimpas.web.jsf.renderers.html.ajax.AjaxHtmlRenderer;

import org.apache.myfaces.renderkit.html.ext.HtmlGridRenderer;

public class AjaxPanelGridRenderer extends AjaxHtmlRenderer implements AjaxRendererBase {

	@Override
	protected Renderer getRenderer(FacesContext context, UIComponent component) {
		return new AjaxHtmlPanelGridRendererHelper();
	}
	
	// hax
	private static class AjaxHtmlPanelGridRendererHelper extends HtmlGridRenderer {
		
		@Override
		public void encodeEnd(FacesContext context, UIComponent component) throws IOException {
			/**
			 * from myfaces 1.1.5
			 */

	        int columns = ((HtmlPanelGrid)component).getColumns();

	        if (columns <= 0)
	            columns = 1;

	        ResponseWriter writer = context.getResponseWriter();

	        // theader and tfooter are rendered before the tbody 
	        renderHeaderOrFooter(context, writer, component, columns, true);   //Header facet
	        renderHeaderOrFooter(context, writer, component, columns, false);  //Footer facet
	        renderChildren(context, writer, component, columns);
		}
	}
}